package com.zlt.miaosha.controller;

import com.zlt.miaosha.redis.RedisService;
import com.zlt.miaosha.result.CodeMsg;
import com.zlt.miaosha.result.Result;
import com.zlt.miaosha.service.MiaoshaUserService;
import com.zlt.miaosha.service.UserService;
import com.zlt.miaosha.util.ValidatorUtil;
import com.zlt.miaosha.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author zhoulitong
 * @date 2018/6/13
 * @description
 */
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
    @Autowired
    MiaoshaUserService miaoshaUserService;

    @GetMapping("/to_login")
    public String toLogin() {
        return "login";
    }

//    @PostMapping("/do_login")
//    @ResponseBody
//    public Result<Boolean> doLogin(@Valid LoginVo loginVo, HttpServletResponse response) {
//        log.info(loginVo.toString());
//        //登录
//        miaoshaUserService.login(loginVo,response);
//        return Result.success(true);
//    }

    //todo 批量生产token
    @PostMapping("/do_login")
    @ResponseBody
    public Result<String> doLogin(@Valid LoginVo loginVo, HttpServletResponse response) {
        log.info(loginVo.toString());
        //登录
        String token = miaoshaUserService.login(loginVo,response);
        return Result.success(token);
    }
}
